<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>layout 管理系统大布局 - Layui</title>
  <link rel="stylesheet" href="/static/libs/layui/css/layui.css">
</head>

<body>
  <div>
    <div class="layui-container-fluid">
      <!-- 面包屑导航 -->
      <span class="layui-breadcrumb">
        <a href="/main">首页</a>
        <a href="/itemList">商品管理</a>
        <a><cite>增加商品</cite></a>
      </span>
      <div class="content" style="margin-top: 20px;max-width: 800px">
        <form class="layui-form" action="">
          <div class="layui-form-item">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-block">
              <input type="text" name="itemName" required lay-verify="required" placeholder="请输入标题" autocomplete="off"
                class="layui-input">
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">商品价格</label>
            <div class="layui-input-block">
              <input type="number" min="0" name="itemPrice" required lay-verify="required" placeholder="请输入价格"
                autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">库存</label>
            <div class="layui-input-block">
              <input type="number" min="0" name="itemNum" value="0" required lay-verify="required" placeholder="请输入库存"
                autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
              <textarea name="itemDesc" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
          </div>
          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">商品图片</label>
            <div class="layui-input-block">
              <button type="button" class="layui-btn" id="upload">
                <i class="layui-icon">&#xe67c;</i>上传图片
              </button>
              <img src="" width="200" id="img" alt="">
            </div>
          </div>
          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">商品详情</label>
            <div class="layui-input-block">
              <div id="content"></div>
            </div>
          </div>
          <div class="layui-form-item">
            <div class="layui-input-block">
              <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
              <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
  <script src="/static/libs/layui/layui.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/wangeditor@latest/dist/wangEditor.min.js"></script>
  <script>
    //Demo
    layui.use(['form', 'upload', 'jquery', 'layer'], function () {
      var form = layui.form;
      var upload = layui.upload;
      var $ = layui.jquery;
      var layer = layui.layer;
      var thumb = ''; // 保存上传成功后的地址
      
      // 初始化富文本
      const E = window.wangEditor
      const editor = new E('#content')
      // 或者 const editor = new E( document.getElementById('div1') )
      editor.create()

      //监听提交
      form.on('submit(formDemo)', function (data) {
        var field = data.field;
        field.itemThumb = thumb; // 上传图片地址
        field.content = editor.txt.html(); // 富文本编辑器的内容
        // 发送请求了
        $.ajax({
          url: '/itemAdd',
          type: 'POST',
          data: field,
          success: function(res){
            if(res.code === 0 ) {
              layer.msg(res.msg, {icon: 1,time:1000}, function(){
                history.go(0);
              })
            }else{
              layer.msg(res.msg, {icon: 2,time:1000})
            }
          }
        })
        return false;
      });
      // 创建上传文件控件
      //执行实例
      var uploadInst = upload.render({
        elem: '#upload' //绑定元素
        ,field: 'file'
        , url: '/upload/' //上传接口
        , done: function (res) {
          //上传完毕回调
          console.log(res);
          $('#img').attr('src', res.data.filePath);
          thumb = res.data.filePath;
        }
        , error: function () {
          //请求异常回调
        }
      });
    });
  </script>
</body>

</html>